package com.lightsaber.trade.as.web.retail;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.lightsaber.trade.as.entity.reta.OrderTemplate;
import com.lightsaber.trade.as.service.reta.OrderTemplateManager;
import com.lightsaber.trade.core.common.util.Calc;
import com.lightsaber.trade.core.web.BaseAction;

@Namespace("/retail")
public class OrderTemplateAction extends BaseAction {

    @Autowired
    private OrderTemplateManager orderTemplateManager;

    @Value(value = "#{appProperties['retail.page.size']}")
    private String PAGE_SIZE;

    private Page<OrderTemplate> page;
    private boolean clear;
    private OrderTemplate orderTemplate;

    @Override
    public String execute() throws Exception {
        if (clear) {
            orderTemplate = null;
        }

        if (page == null) {// Click 'Search' button first
            page = new Page<OrderTemplate>(Integer.valueOf(PAGE_SIZE));
        } else {// Click 'Search' button
            page.setPageSize(Integer.valueOf(PAGE_SIZE));
        }

        if (!page.isOrderBySetted()) {
            page.setOrderBy("name");
            page.setOrder(Page.ASC);
        }

        orderTemplateManager.findPage(orderTemplate, page);
        return SUCCESS;
    }

    public String edit() throws Exception {
        orderTemplate = orderTemplateManager.get(orderTemplate.getId());

        return INPUT;
    }

    public String delete() throws Exception {
        orderTemplateManager.delete(orderTemplate.getId());

        return execute();
    }

    public String save() throws Exception {
        orderTemplate = orderTemplateManager.saveOrderTemplate(orderTemplate);

        return INPUT;
    }

    public String add() {
        return INPUT;
    }

    public void ajaxGetTotalPrice() {
        orderTemplate = orderTemplateManager.get(orderTemplate.getId());

        Struts2Utils.renderText(Calc.leaveTwoDigital(orderTemplate.getTotalPrice()));
    }

    public Page<OrderTemplate> getPage() {
        return page;
    }

    public void setPage(Page<OrderTemplate> page) {
        this.page = page;
    }

    public boolean isClear() {
        return clear;
    }

    public void setClear(boolean clear) {
        this.clear = clear;
    }

    public OrderTemplate getOrderTemplate() {
        return orderTemplate;
    }

    public void setOrderTemplate(OrderTemplate orderTemplate) {
        this.orderTemplate = orderTemplate;
    }
}
